﻿using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Notifications;
using Windows.UI.Popups;

namespace BuddyTracker.ViewModel.Helpers
{
    /// <summary>
    /// Helper class with all static methods for displaying messages.
    /// </summary>
    public class MessageDialogHelper
    {
        /// <summary>
        /// Loader for dictionaries resources.
        /// </summary>
        private static ResourceLoader loader = new ResourceLoader();

        /// <summary>
        /// No constructor
        /// </summary>
        private MessageDialogHelper() { }

        /// <summary>
        /// Shows a basic message box with OK button only.
        /// </summary>
        /// <param name="content">Message text.</param>
        /// <param name="title">MessageBox title.</param>
        public static async void Show(string content, string title)
        {
            try
            {
                MessageDialog messageDialog = new MessageDialog(content, title);
                UICommand okBtn = new UICommand(loader.GetString("Ok"));
                messageDialog.Commands.Add(okBtn);
                await messageDialog.ShowAsync();
            }
            catch
            {
                // probably AccessDenied, because one is already on, no matter...
            }
        }



        /// <summary>
        /// Basic message box, but async.
        /// </summary>
        public static IAsyncOperation<IUICommand> ShowAsync(string content, string title)
        {
            try
            {
                MessageDialog messageDialog = new MessageDialog(content, title);
                UICommand okBtn = new UICommand(loader.GetString("Ok"));
                messageDialog.Commands.Add(okBtn);
                return messageDialog.ShowAsync();
            }
            catch
            {
                // probably AccessDenied, because one is already on, no matter...
                return null;
            }

        }

        public static async Task ShowYesNoDialog(string content, string title, UICommandInvokedHandler Yes, UICommandInvokedHandler No)
        {
            try
            {
                MessageDialog msgDialog = new MessageDialog(content, title);

                UICommand okBtn = new UICommand(loader.GetString("yes"));
                okBtn.Invoked = Yes;
                msgDialog.Commands.Add(okBtn);

                UICommand cancelBtn = new UICommand(loader.GetString("no"));
                cancelBtn.Invoked = No;
                msgDialog.Commands.Add(cancelBtn);

                await msgDialog.ShowAsync();
            }
            catch
            {
                // probably AccessDenied, because one is already on, no matter...
            }

        }

        public static async void ShowGPSOffMessage()
        {
            try
            {
                MessageDialog msgDialog = new MessageDialog(loader.GetString("GpsOff"), loader.GetString("Error"));

                UICommand okBtn = new UICommand("ok");
                msgDialog.Commands.Add(okBtn);

                UICommand cancelBtn = new UICommand(loader.GetString("settings"));
                cancelBtn.Invoked = async delegate
                {
                    await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));

                };
                msgDialog.Commands.Add(cancelBtn);

                await msgDialog.ShowAsync();
            }
            catch
            {
                //probably access denied
            }

        }

        /// <summary>
        /// Shows error message with the option to send support mail.
        /// </summary>
        /// <param name="errorMessage"></param>
        public async static void ShowErrorMessage(string errorMessage, Exception e)
        {
            try
            {
                MessageDialog msgDialog = new MessageDialog(errorMessage, loader.GetString("Error"));

                UICommand okBtn = new UICommand("ok");
                msgDialog.Commands.Add(okBtn);

                UICommand cancelBtn = new UICommand(loader.GetString("support"));
                cancelBtn.Invoked = async delegate
                {
                    var mailto =
                        new Uri("mailto:?to=nmilosev@live.com&subject=FollowerError&body=" + errorMessage +
                                Environment.NewLine + e.StackTrace.ToString());
                    await Launcher.LaunchUriAsync(mailto);

                };
                msgDialog.Commands.Add(cancelBtn);

                await msgDialog.ShowAsync();
            }
            catch
            {
                //probably access denied
            }
        }


        public static async void ShowOkCancelDialog(string content, string title, UICommandInvokedHandler OKHandler)
        {
            MessageDialog msgDialog = new MessageDialog(content, title);

            UICommand okBtn = new UICommand("ok");
            okBtn.Invoked = OKHandler;
            msgDialog.Commands.Add(okBtn);

            UICommand cancelBtn = new UICommand("cancel");
            cancelBtn.Invoked = delegate { }; //do nothing
            msgDialog.Commands.Add(cancelBtn);

            await msgDialog.ShowAsync();

        }

        public static async Task<bool> ShowYesNoDialogWithResult(string content, string title)
        {
            MessageDialog msgDialog = new MessageDialog(content, title);

            bool value = false;

            UICommand okBtn = new UICommand(loader.GetString("yes"));
            okBtn.Invoked = delegate { value = true; };
            msgDialog.Commands.Add(okBtn);

            UICommand cancelBtn = new UICommand(loader.GetString("no"));
            cancelBtn.Invoked = delegate { value = false; };
            msgDialog.Commands.Add(cancelBtn);

            await msgDialog.ShowAsync();
            return value;

        }
        /// <summary>
        /// Makes toast (not that one...)
        /// </summary>
        /// <param name="message">Toast message</param>
        public static void MakeNotification(string message)
        {
            var toastXmlString = string.Format("<toast><visual version='1'><binding template='ToastText01'><text id='1'>{0}</text></binding></visual></toast>", message);
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(toastXmlString);
            var toast = new ToastNotification(xmlDoc);
            ToastNotificationManager.CreateToastNotifier().Show(toast);
        }
    }
}
